<script setup>
import { ref, onMounted } from 'vue'
import { getMyDonations } from '@/api/donation'
import { ElMessage } from 'element-plus'

const donations = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

const loadDonations = async () => {
  loading.value = true
  try {
    const res = await getMyDonations({
      page: currentPage.value,
      size: pageSize.value
    })
    if (res.code === 200) {
      donations.value = res.data.list
      total.value = res.data.total
    } else {
      ElMessage.error(res.msg || '获取捐赠记录失败')
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '获取捐赠记录失败')
  } finally {
    loading.value = false
  }
}

const handlePageChange = (page) => {
  currentPage.value = page
  loadDonations()
}

const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
  loadDonations()
}

onMounted(() => {
  loadDonations()
})
</script>

<template>
  <div class="donation-list-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>我的捐赠记录</h2>
        </div>
      </template>
      
      <el-table
        :data="donations"
        v-loading="loading"
        style="width: 100%"
      >
        <el-table-column
          prop="type"
          label="捐赠类型"
          width="100"
        />
        
        <el-table-column
          prop="amount"
          label="捐赠金额"
          width="150"
        >
          <template #default="{ row }">
            <span v-if="row.type === '金额'">¥{{ row.amount }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column
          prop="itemDesc"
          label="物品描述"
        >
          <template #default="{ row }">
            <span v-if="row.type === '物品'">{{ row.itemDesc }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column
          prop="donateTime"
          label="捐赠时间"
          width="180"
        />
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:pageSize="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.donation-list-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 